package com.dolphin.fly.common;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 分页器封
 * 
 * @author LI.T
 * 
 */
public class Pageable<T> implements Serializable {

    private static final long serialVersionUID = 6217047497903290688L;

    /**
     * 每页大小
     */
    private int pageSize;

    /**
     * 记录总条
     */
    private int total = 0;

    /**
     * 记录总条
     */
    private int totalPage = 1;

    /**
     * 当前页号
     */
    private int pageNo = 1;

    /**
     * 本页数据
     */
    private List<T> rows = Collections.emptyList();
    
    public Pageable() {
        super();
    }
    
    /**
     * 
     * @param data
     * @param total
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Pageable<T> instanceByPageNo(List<T> data, int total, int pageNumber, int pageSize) {
        this.pageSize = pageSize;
        this.total = total;
        this.pageNo = pageNumber;
        if(data!=null){
            this.rows = data;
        }
    	return this;
    }
    
    /**
     * 
     */
    public Pageable<T> instanceByStartRow(List<T> data,  int total, int startRow, int offset){
    	if(offset != 0){
    	    pageNo = startRow/offset + 1 ;
    		pageSize = offset ;
    		totalPage = total/offset + 1;
    	}else{
    	    pageNo = 1;
    		pageSize = total;
    		totalPage = 1;
    	}
        if(data!=null){
            this.rows = data;
        }
    	this.total = total ;
    	return this;
    }
    
    public int getStartRow() {
        return (pageNo-1) * pageSize;
    }
    
    public int getOffset() {
        return pageSize;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getTotal() {
        return total;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getPageNo() {
        return pageNo;
    }

    public int getPageNumber() {
        return pageNo;
    }

    public List<T> getData() {
        return rows;
    }

    public List<T> getRows() {
        return rows;
    }

}
